package com.example.messangersms;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Register extends Activity {

	String passwordString = "";
	String emailString = "";
	Context context;
	String result = "";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_register);
		
		Button submitBtn = (Button) findViewById(R.id.sign_in_button);
		submitBtn.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				
				EditText email = (EditText) findViewById(R.id.email);
				emailString = email.getText().toString();
				
				EditText password = (EditText) findViewById(R.id.password);
				passwordString = password.getText().toString();
				
				if(!passwordString.equals("") && !emailString.equals("")) 
				{
					//tryRegister(emailString, passwordString);
					register tryRegister = new register();
					tryRegister.execute();
					
					returnResult();
				} else {
					context.getApplicationContext();
					Toast toast = Toast.makeText(context, "Must input an email and password",
							400);
					toast.show(); 
				}
			}
		});

	}

	private void returnResult() {
			while(result.equals("")){
				
			}
				context = getApplicationContext();
				Toast toast = Toast.makeText(context, result,
					Toast.LENGTH_LONG);
				toast.show();
				result = "";
	}		

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_register, menu);
		return true;
	}

	
	private class register extends AsyncTask<Void, Void, String> {

		@Override
		protected String doInBackground(Void... params) {
			
			System.out.println(emailString + "  " + passwordString);
			HttpClient httpclient = new DefaultHttpClient();
			HttpPost httppost = new HttpPost(
					"http://softeng.cs.uwosh.edu/students/cs342g5/functions.php");

			try {
				List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
						3);
				nameValuePairs.add(new BasicNameValuePair(
						"tag", "register"));
				nameValuePairs.add(new BasicNameValuePair(
						"email", emailString));
				nameValuePairs.add(new BasicNameValuePair(
						"password", passwordString));
				httppost.setEntity(new UrlEncodedFormEntity(
						nameValuePairs));
				
				// Execute HTTP Post Request
				System.out.println(EntityUtils.toString(httppost
						.getEntity()));
				HttpResponse response = httpclient
						.execute(httppost);
				result = EntityUtils.toString(response
						.getEntity());
				System.out.println("This is the result: "+result);
				
			} catch (ClientProtocolException e) {

			} catch (IOException e) {

			}
			return result;
		}
	  @Override
	  protected void onPostExecute(String result) {
		  context.getApplicationContext();
		  if(result.contains("User already existed")){
			  Toast toast = Toast.makeText(context, "Sorry, that user already exists. Please try another username!",
						1500);
				toast.show();
		  }else if(result.contains("success")){
			  Toast toast = Toast.makeText(context, "Registration was successful!",
						1500);
				toast.show();
		  }else{
			  //Temporary until my check works better
			  Toast toast = Toast.makeText(context, "Registration was successful!",
						1500);
				toast.show();
		  }
		  
			 
	  }

		
		
	}
}


